package com.jinhua.huaojbackendmodel.model.vo;

import cn.hutool.json.JSONUtil;
import com.jinhua.huaojbackendmodel.model.codesandbox.JudgeInfo;
import com.jinhua.huaojbackendmodel.model.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * 题目视图（已脱敏）
 *

 */
@Data
public class QuestionSubmitVO implements Serializable {

    private Long id;

    /**
     * 题目 id
     */
    private Long questionId;

    /**
     * 题目
     */
    private String title;

    /**
     * 提交用户 id
     */
    private Long userId;

    /**
     * 编程语言
     */
    private String language;

    /**
     * 提交代码
     */
    private String code;

    /**
     * 提交状态(0 -带判题，1 - 判题中，2 - 判题成功，3 - 判题失败)
     */
    private int state;

    /**
     * 判题信息(执行时间，占用内存，执行信息)
     */
    private JudgeInfo judgeInfo;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;


    /**
     * 包装类转对象
     *
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        //参数是否合法

        if (questionSubmitVO == null) {
            return null;
        }
        //进行自动转换 无法自动转换的字段judgeInfo
        QuestionSubmit questionSubmit = new QuestionSubmit();
        BeanUtils.copyProperties(questionSubmitVO,questionSubmit);
        JudgeInfo judgeInfoJSON = questionSubmitVO.getJudgeInfo();
        String judgeInfo = JSONUtil.toJsonStr(judgeInfoJSON);
        questionSubmit.setJudgeInfo(judgeInfo);

        return questionSubmit;
    }

    /**
     * 对象转包装类
     *
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        //参数是否合法
        if (questionSubmit == null) {
            return null;
        }
        //进行自动转换 无法自动转换的字段有title、judgeInfo
        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVO);
        //转换judgeInfo字段
        String judgeInfoJSON = questionSubmit.getJudgeInfo();
        questionSubmitVO.setJudgeInfo(JSONUtil.toBean(judgeInfoJSON, JudgeInfo.class));
        //返回的VO类需要查询数据库得到question 然后设置title字段
        return questionSubmitVO;
    }
}
